package com.du.annulrview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;

import java.util.List;

/**
 * Created by DuZiLin on 2018/5/8.
 */

public class SplitAnnulrView extends View{
    private List<ProgressBean> progressBeans;
    private int viewSize;
    private Paint mPaint;
    private Paint mTextPaint;
    private float annulrWidth;
    private int annulrColor;
    private int textSize;
    private int textColor;
    private Context mContext;
    private float[] mProgress = {};
    private boolean canDrawText = false;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            SplitAnnulrView.this.postInvalidate();
        }
    };

    public SplitAnnulrView(Context context) {
        super(context);
        mContext = context;
        init(null);
    }



    public SplitAnnulrView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init(attrs);
    }

    public SplitAnnulrView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init(attrs);
    }

    private void init(AttributeSet attrs) {

        if(attrs != null){
            TypedArray typedArray = mContext.obtainStyledAttributes(attrs , R.styleable.SplitAnnulrView);
            annulrWidth = typedArray.getDimension(R.styleable.SplitAnnulrView_split_annulr_width , 100);
            textSize = (int) typedArray.getDimension(R.styleable.SplitAnnulrView_textsize , 60);
            textColor = typedArray.getColor(R.styleable.SplitAnnulrView_textcolor , Color.BLACK);
            typedArray.recycle();
        }
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setAntiAlias(true);
        mPaint.setColor(annulrColor);
        mPaint.setStrokeWidth(annulrWidth);

        mTextPaint = new Paint();
        mTextPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mTextPaint.setStrokeWidth(textSize/15);
        mTextPaint.setAntiAlias(true);
        mTextPaint.setColor(textColor);
        mTextPaint.setTextSize(textSize);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int mode = MeasureSpec.getMode(widthMeasureSpec);
        switch (mode) {
            case MeasureSpec.AT_MOST:
                viewSize = 500;
                break;
            case MeasureSpec.EXACTLY:
            case MeasureSpec.UNSPECIFIED:
                viewSize = MeasureSpec.getSize(widthMeasureSpec) > MeasureSpec.getSize(heightMeasureSpec) ? MeasureSpec.getSize(heightMeasureSpec) : MeasureSpec.getSize(widthMeasureSpec);
                break;
        }
        setMeasuredDimension(viewSize, viewSize);
    }

    
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        RectF rectF = new RectF(annulrWidth/2 , annulrWidth/2 , viewSize - annulrWidth/2 , viewSize - annulrWidth/2);
        float startAngle = -90;
        for (int i = 0; i < progressBeans.size(); i++) {
            mPaint.setColor(progressBeans.get(i).getCloor());
            canvas.drawArc(rectF , startAngle , mProgress[i] , false , mPaint);
            startAngle += progressBeans.get(i).getProgress()/100*360;

        }

        if (canDrawText){
            float textStartAngle = -90;
            for (int i = 0; i < progressBeans.size(); i++) {
                Path path = new Path();
                path.addArc(rectF , textStartAngle ,progressBeans.get(i).getProgress() /100 *360 );
                canvas.drawTextOnPath(progressBeans.get(i).getProgress()+"%" , path , 0,(annulrWidth-textSize)/2,mTextPaint);
                textStartAngle += progressBeans.get(i).getProgress()/100*360;
            }
        }

    }

    public List<ProgressBean> getProgressBeans() {
        return progressBeans;
    }

    public void setProgressBeans(final List<ProgressBean> progressBeans) {
            this.progressBeans = progressBeans;
            mProgress = new float[progressBeans.size()];
            new Thread(){
                @Override
                public void run() {
                    super.run();
                    for (int i = 0 ; i<progressBeans.size() ; i++){
                        while(mProgress[i] < progressBeans.get(i).getProgress()/100*360){
                            mProgress[i]+=0.2;
                            try {
                                sleep(1);
                                mHandler.sendEmptyMessage(0);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    canDrawText = true;
                }
            }.start();
    }
}
